package club.yunqiang.rabbitmqapi.quickstart;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException {
        // 1、创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        // 2、通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();

        // 3、通过 Connection 创建信道 Channel
        Channel channel = connection.createChannel();

        // 4、通过 Channel 发送数据
        String msg = "Hello RabbitMQ!";
        for (int i = 0; i < 5; i++) {
            channel.basicPublish("", "test001", null, (msg + i).getBytes());
        }

        // 5、关闭连接
        channel.close();
        connection.close();
    }

}
